/*
 * @Author: yangzr 1076420747@qq.com
 * @Date: 2022-08-26 17:57:04
 * @LastEditors: yangzr 1076420747@qq.com
 * @LastEditTime: 2023-05-05 19:08:02
 * @FilePath: \ktgj-crm\src\config\axios.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import setting from './setting'
import { Message } from 'element-ui';
import axios from 'axios';
import router from '@/router';
import qs from 'qs';
import cookie from 'js-cookie'
// import Router from './router'
// import storage from '../common/Helper/storage'

// 创建 axios 实例
let service = axios.create(setting)

// 设置 post 默认 Content-Type
service.defaults.headers.post['Content-Type'] = 'application/json';

// 添加请求拦截器
service.interceptors.request.use(
    (config) => {
        config.headers.token = cookie.get('Xd_Token');
        
        //判断请求方式是否为POST，进行转换格式
        // console.log('config', config);
        /*   config.method === 'post' ?
               config.data = {...config.data }:
               config.params = {...config.params }; */
        // 请求发送前进行处理
        return config
    },
    (error) => {
        // 请求错误处理
        return Promise.reject(error)
    }
)
// 添加响应拦截器
service.interceptors.response.use(
    
    (response) => {
        console.log(response)
        if (response.data.code == "00000"||response.data.code == "200") {
            let { data } = response;
            return data;
        } else if(response.data.code == "-1" || response.data.code == "00004"){
            Message.error({
                message: response.data.code + ":" + response.data.msg,
                offset: 100
            })
          cookie.remove('Xd_Token')
            router.replace({
                path: '/login'
            })
          return {};

        }else{
            // Message.error(response.data.code + ":" + response.data.msg);
            Message.error({
                message: response.data.code + ":" + response.data.msg,
                offset: 100
            })
            return {};
        }
    },
    (error) => {
        console.log(error)
        // let info = {},
        //     { status, statusText, data } = error.response;
        // //response为空时
        // if (!error.response) {
        //     Message.error("response为空,5000:" + 'Network Error');
        // } else {
        //     //报错
        //     Message.error(status + ":" + statusText);
        // }
    }
)


/**
 * 创建统一封装过的 axios 实例
 * @return {AxiosInstance}
 */
export default service